package ldkxingzhe.ldkxingzhedemo.touchevent;

import android.app.Activity;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by liangdengke on 2015/9/7.
 */
public class OnSwipeTouchListener implements View.OnTouchListener{

    private final GestureDetector mGestureDetector;
    private Context mContext;

    static interface OnSwipeListener{
        void onSwipeRight();
        void onSwipeLeft();
        void onSwipeBottom();
        void onSwipeTop();
    }
    private OnSwipeListener mSwipeListener = new OnSwipeListener() {
        @Override
        public void onSwipeRight() {
            if(mContext instanceof Activity){
                ((Activity)mContext).onBackPressed();
            }
        }

        @Override
        public void onSwipeLeft() {

        }

        @Override
        public void onSwipeBottom() {

        }

        @Override
        public void onSwipeTop() {

        }
    };

    /**
     * 设置mSwipeListener
     *
     * @param swipeListener 要设置的mSwipeListener
     */
    public void setSwipeListener(OnSwipeListener swipeListener) {
        if(swipeListener != null){
            mSwipeListener = swipeListener;
        }
    }

    public OnSwipeTouchListener(Context context){
        mContext = context;
        mGestureDetector = new GestureDetector(context,new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener{
        private static final int SWIPE_THRESHOLD= 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try{
                float diffX = e2.getX() - e1.getX();
                float diffY = e2.getY() - e1.getY();
                if(Math.abs(diffX) > Math.abs(diffY)){
                    if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD){
                        if(diffX > 0){
                            mSwipeListener.onSwipeRight();
                        }else{
                            mSwipeListener.onSwipeLeft();
                        }
                    }
                    result = true;
                }else{
                    if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD){
                        if(diffY > 0){
                            mSwipeListener.onSwipeBottom();
                        }else{
                            mSwipeListener.onSwipeTop();
                        }
                    }
                    result = true;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return result;
        }
    }
}
